package com.inspur.s57.datacatalog.controller;

import com.inspur.s57.datacatalog.entity.CataLog;
import com.inspur.s57.datacatalog.entity.DataAttribute;
import com.inspur.s57.datacatalog.service.CataLogService;
import com.inspur.s57.datacatalog.service.DataAttributeService;
import com.inspur.s57.datacatalog.utils.ResponseUtil;
import com.inspur.s57.datacatalog.utils.Select2Util;
import com.inspur.s57.datacatalog.vo.DataTablesVO;
import com.inspur.s57.datacatalog.vo.QueryVO;
import com.inspur.s57.datacatalog.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/catalog")
public class CataLogController {

    @Autowired
    private CataLogService cataLogService;

    @Autowired
    private DataAttributeService attributeService;

    /**
     * 分页查询类目
     * @param queryVO
     * @return
     */
    @PostMapping("findCataLogByPage")
    public DataTablesVO<CataLog> findCataLogByPage(QueryVO queryVO){
        Integer pageNo=queryVO.getStart()/queryVO.getLength();
        Pageable pageable= PageRequest.of(pageNo,queryVO.getLength());
        Page<CataLog> page =cataLogService.findCataLogByPage(queryVO.getSearch(),pageable);
        DataTablesVO<CataLog> result=new DataTablesVO<>(page.getContent(),page.getTotalElements(),page.getTotalElements(),queryVO.getDraw());
        return result;
    }

    /**
     * 类目属性分页
     * @param queryVO
     * @return
     */
    @PostMapping("findDataAttributeByPage")
    public DataTablesVO<DataAttribute> findDataAttributeByPage(QueryVO queryVO){
        Integer pageNo=queryVO.getStart()/queryVO.getLength();
        Pageable pageable= PageRequest.of(pageNo,queryVO.getLength());
        Page<DataAttribute> page =attributeService.findDataAttributesByClassIdAndNameLike(queryVO.getClassId(),queryVO.getSearch(),pageable);
        DataTablesVO<DataAttribute> result=new DataTablesVO<>(page.getContent(),page.getTotalElements(),page.getTotalElements(),queryVO.getDraw());
        return result;
    }

    /**
     * 根据参数Id，获取类目
     * @param id
     * @return
     */
    @GetMapping("findCatLogById")
    public ResponseEntity<ResponseVO> findCatLogById(@RequestParam(value = "id") Integer id){
        CataLog cataLog=cataLogService.findCataLogById(id);
        return ResponseUtil.success(cataLog);
    }

    /**
     * 类目创建
     * @param cataLog
     * @return
     */
    @PostMapping("createCataLog")
    public ResponseEntity<ResponseVO> createCataLog(@RequestBody CataLog cataLog){
        boolean isSave=cataLogService.saveCataLog(cataLog);
        return ResponseUtil.success(isSave);
    }

    /**
     * 删除类目
     * @param id
     * @return
     */
    @GetMapping("deleteCatLogById")
    public ResponseEntity<ResponseVO> deleteCatLogById(@RequestParam(value = "id") Integer id){
        boolean isSave=cataLogService.deleteCataLogById(id);
        return ResponseUtil.success(isSave);
    }

    /**
     * 查询类目
     * @return
     */
    @GetMapping("findAllCataLog")
    public ResponseEntity<ResponseVO> findAllCataLog(){
        List<CataLog> cataLogs= cataLogService.findAllCataLog();
        return ResponseUtil.success(cataLogs);
    }

    /**
     * 查询类目
     * @return
     */
    @GetMapping("findAllCataLogToSelect2")
    public ResponseEntity<ResponseVO> findAllCataLogToSelect2(){
        List<CataLog> cataLogs= cataLogService.findAllCataLog();
        return ResponseUtil.success(Select2Util.CataLog2Select2VO(cataLogs));
    }

    /**
     * 保存类目属性
     * @param attribute
     * @return
     */
    @PostMapping("saveAttribute")
    public ResponseEntity<ResponseVO> saveAttribute(@RequestBody DataAttribute attribute){
        boolean isSave=attributeService.saveAttribute(attribute);
        return ResponseUtil.success(isSave);
    }

    /**
     * 根据Id，删除类目
     * @param id
     * @return
     */
    @PostMapping("deleteCatalog")
    public ResponseEntity<ResponseVO> deleteCatalog(@RequestParam(value = "id") Integer id){
        boolean isDel=cataLogService.deleteCataLogById(id);
        return ResponseUtil.success(isDel);
    }

    /**
     * 根据Id，获取类目属性
     * @param id
     * @return
     */
    @PostMapping ("findAttributeById")
    public ResponseEntity<ResponseVO> findAttributeById(@RequestParam(value = "id") Integer id){
        DataAttribute attribute=attributeService.findDataAttributeById(id);
        return ResponseUtil.success(attribute);
    }

    /**
     * 根据ID，删除类目属性
     * @param id
     * @return
     */
    @PostMapping("deleteAttribute")
    public ResponseEntity<ResponseVO> deleteAttribute(@RequestParam(value = "id") Integer id){
        boolean isDel=attributeService.deleteAttributeById(id);
        return ResponseUtil.success(isDel);
    }

    /**
     * 根据ClassId,获取类目属性
     * @param classId
     * @return
     */
    @PostMapping("findDataAttributesByClassId")
    public ResponseEntity<ResponseVO> findDataAttributesByClassId(@RequestParam(value = "classId") Integer classId){

        return ResponseUtil.success(attributeService.findDataAttributesByClassId(classId));
    }
}
